import { createRouter, createWebHistory } from 'vue-router'
import HomeRoot from '../layout/HomeRootView.vue'
import PageRoot from '../layout/PageRootView.vue'
import MaterialRouter from "./material/index"
import UserRouter from "./user/index"
import MemberRouter from "./member/index"
import RecordRouter from "./record/index"
import LoanRouter from "./loan/index"
import MachineRouter from "./machine/index"

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL), routes: [{
        path: '/home', name: 'homeRoot', component: HomeRoot, children: [{
            path: '/', name: 'home', meta: {
                title: "首页"
            }, component: () => import('@/views/Home/HomeView.vue')
        }, {
            path: '/apps', name: 'apps', meta: {
                title: "应用中心"
            }, component: () => import('@/views/Apps/AppsView.vue')
        }, {
            path: '/user', name: 'user', meta: {
                title: "我的"
            }, component: () => import('@/views/User/UserView.vue')
        },]
    }, {
        path: '/',
        name: 'page',
        component: PageRoot,
        children: [...MaterialRouter, ...UserRouter, ...MemberRouter, ...RecordRouter, ...LoanRouter, ...MachineRouter, {
            path: '/update-log', name: 'updateLog', meta: {
                title: '更新日志', hideTabbar: true, noAuth: true
            }, component: () => import('@/views/Home/UpdateLog.vue')
        }]
    }, {
        path: '/login', name: 'login', meta: {
            title: "登录",
        }, component: () => import('@/views/LoginView.vue')
    }]
})

router.beforeEach((to, from, next) => {
    if (Boolean(Number(localStorage.getItem('isLogin'))) === false && to.name !== 'login' && !to.meta.noAuth) {
        next({ name: 'login' })
    }
    if (to.meta.title) {
        document.title = to.meta.title + ' - 广城大数控制造协会'
    }
    next()
})

export default router
